<h1 id="绘图">绘图</h1>
<p>PyQt5绘图系统能渲染矢量图像、位图图像和轮廓字体文本。一般会使用在修改或者提高现有组件的功能，或者创建自己的组件。使用PyQt5的绘图API进行操作。</p>
<p>绘图由<code>paintEvent()</code>方法完成，绘图的代码要放在<code>QPainter</code>对象的<code>begin()</code>和<code>end()</code>方法之间。是低级接口。</p>
<h2 id="文本涂鸦">文本涂鸦</h2>
<p>我们从画一些Unicode文本开始。</p>
<div class="sourceCode" id="cb1"><pre><code class="language-python"><a class="sourceLine" id="cb1-1" data-line-number="1"><span class="co">#!/usr/bin/python3</span></a>
<a class="sourceLine" id="cb1-2" data-line-number="2"><span class="co"># -*- coding: utf-8 -*-</span></a>
<a class="sourceLine" id="cb1-3" data-line-number="3"></a>
<a class="sourceLine" id="cb1-4" data-line-number="4"><span class="co">&quot;&quot;&quot;</span></a>
<a class="sourceLine" id="cb1-5" data-line-number="5"><span class="co">ZetCode PyQt5 tutorial </span></a>
<a class="sourceLine" id="cb1-6" data-line-number="6"></a>
<a class="sourceLine" id="cb1-7" data-line-number="7"><span class="co">In this example, we draw text in Russian Cylliric.</span></a>
<a class="sourceLine" id="cb1-8" data-line-number="8"></a>
<a class="sourceLine" id="cb1-9" data-line-number="9"><span class="co">Author: Jan Bodnar</span></a>
<a class="sourceLine" id="cb1-10" data-line-number="10"><span class="co">Website: zetcode.com </span></a>
<a class="sourceLine" id="cb1-11" data-line-number="11"><span class="co">Last edited: August 2017</span></a>
<a class="sourceLine" id="cb1-12" data-line-number="12"><span class="co">&quot;&quot;&quot;</span></a>
<a class="sourceLine" id="cb1-13" data-line-number="13"></a>
<a class="sourceLine" id="cb1-14" data-line-number="14"><span class="im">import</span> sys</a>
<a class="sourceLine" id="cb1-15" data-line-number="15"><span class="im">from</span> PyQt5.QtWidgets <span class="im">import</span> QWidget, QApplication</a>
<a class="sourceLine" id="cb1-16" data-line-number="16"><span class="im">from</span> PyQt5.QtGui <span class="im">import</span> QPainter, QColor, QFont</a>
<a class="sourceLine" id="cb1-17" data-line-number="17"><span class="im">from</span> PyQt5.QtCore <span class="im">import</span> Qt</a>
<a class="sourceLine" id="cb1-18" data-line-number="18"></a>
<a class="sourceLine" id="cb1-19" data-line-number="19"><span class="kw">class</span> Example(QWidget):</a>
<a class="sourceLine" id="cb1-20" data-line-number="20">    </a>
<a class="sourceLine" id="cb1-21" data-line-number="21">    <span class="kw">def</span> <span class="fu">__init__</span>(<span class="va">self</span>):</a>
<a class="sourceLine" id="cb1-22" data-line-number="22">        <span class="bu">super</span>().<span class="fu">__init__</span>()</a>
<a class="sourceLine" id="cb1-23" data-line-number="23">        </a>
<a class="sourceLine" id="cb1-24" data-line-number="24">        <span class="va">self</span>.initUI()</a>
<a class="sourceLine" id="cb1-25" data-line-number="25">        </a>
<a class="sourceLine" id="cb1-26" data-line-number="26">        </a>
<a class="sourceLine" id="cb1-27" data-line-number="27">    <span class="kw">def</span> initUI(<span class="va">self</span>):      </a>
<a class="sourceLine" id="cb1-28" data-line-number="28">        </a>
<a class="sourceLine" id="cb1-29" data-line-number="29">        <span class="va">self</span>.text <span class="op">=</span> <span class="st">&quot;Лев Николаевич Толстой</span><span class="ch">\n</span><span class="st">Анна Каренина&quot;</span></a>
<a class="sourceLine" id="cb1-30" data-line-number="30"></a>
<a class="sourceLine" id="cb1-31" data-line-number="31">        <span class="va">self</span>.setGeometry(<span class="dv">300</span>, <span class="dv">300</span>, <span class="dv">280</span>, <span class="dv">170</span>)</a>
<a class="sourceLine" id="cb1-32" data-line-number="32">        <span class="va">self</span>.setWindowTitle(<span class="st">&#39;Drawing text&#39;</span>)</a>
<a class="sourceLine" id="cb1-33" data-line-number="33">        <span class="va">self</span>.show()</a>
<a class="sourceLine" id="cb1-34" data-line-number="34">        </a>
<a class="sourceLine" id="cb1-35" data-line-number="35"></a>
<a class="sourceLine" id="cb1-36" data-line-number="36">    <span class="kw">def</span> paintEvent(<span class="va">self</span>, event):</a>
<a class="sourceLine" id="cb1-37" data-line-number="37"></a>
<a class="sourceLine" id="cb1-38" data-line-number="38">        qp <span class="op">=</span> QPainter()</a>
<a class="sourceLine" id="cb1-39" data-line-number="39">        qp.begin(<span class="va">self</span>)</a>
<a class="sourceLine" id="cb1-40" data-line-number="40">        <span class="va">self</span>.drawText(event, qp)</a>
<a class="sourceLine" id="cb1-41" data-line-number="41">        qp.end()</a>
<a class="sourceLine" id="cb1-42" data-line-number="42">        </a>
<a class="sourceLine" id="cb1-43" data-line-number="43">        </a>
<a class="sourceLine" id="cb1-44" data-line-number="44">    <span class="kw">def</span> drawText(<span class="va">self</span>, event, qp):</a>
<a class="sourceLine" id="cb1-45" data-line-number="45">      </a>
<a class="sourceLine" id="cb1-46" data-line-number="46">        qp.setPen(QColor(<span class="dv">168</span>, <span class="dv">34</span>, <span class="dv">3</span>))</a>
<a class="sourceLine" id="cb1-47" data-line-number="47">        qp.setFont(QFont(<span class="st">&#39;Decorative&#39;</span>, <span class="dv">10</span>))</a>
<a class="sourceLine" id="cb1-48" data-line-number="48">        qp.drawText(event.rect(), Qt.AlignCenter, <span class="va">self</span>.text)        </a>
<a class="sourceLine" id="cb1-49" data-line-number="49">                </a>
<a class="sourceLine" id="cb1-50" data-line-number="50">        </a>
<a class="sourceLine" id="cb1-51" data-line-number="51"><span class="cf">if</span> <span class="va">__name__</span> <span class="op">==</span> <span class="st">&#39;__main__&#39;</span>:</a>
<a class="sourceLine" id="cb1-52" data-line-number="52">    </a>
<a class="sourceLine" id="cb1-53" data-line-number="53">    app <span class="op">=</span> QApplication(sys.argv)</a>
<a class="sourceLine" id="cb1-54" data-line-number="54">    ex <span class="op">=</span> Example()</a>
<a class="sourceLine" id="cb1-55" data-line-number="55">    sys.exit(app.exec_())</a></code></pre></div>
<p>写了一些文本上下居中对齐的俄罗斯Cylliric语言的文字。</p>
<pre><code class="language-python">def paintEvent(self, event):
...</code></pre>
<p>在绘画事件内完成绘画动作。</p>
<pre><code class="language-python">qp = QPainter()
qp.begin(self)
self.drawText(event, qp)
qp.end()</code></pre>
<p><code>QPainter</code>是低级的绘画类。所有的绘画动作都在这个类的<code>begin()</code>和<code>end()</code>方法之间完成，绘画动作都封装在<code>drawText()</code>内部了。</p>
<pre><code class="language-python">qp.setPen(QColor(168, 34, 3))
qp.setFont(QFont(&#39;Decorative&#39;, 10))</code></pre>
<p>为文字绘画定义了笔和字体。</p>
<pre><code class="language-python">qp.drawText(event.rect(), Qt.AlignCenter, self.text)</code></pre>
<p><code>drawText()</code>方法在窗口里绘制文本，<code>rect()</code>方法返回要更新的矩形区域。</p>
<p>程序展示：</p>
<figure>
<img class="whitelist" src="docs/PyQt5/images/9-drawtext.png" alt="drawing text" />
</figure>
<h2 id="点的绘画">点的绘画</h2>
<p>点是最简单的绘画了。</p>
<div class="sourceCode" id="cb6"><pre><code class="language-python"><a class="sourceLine" id="cb6-1" data-line-number="1"><span class="co">#!/usr/bin/python3</span></a>
<a class="sourceLine" id="cb6-2" data-line-number="2"><span class="co"># -*- coding: utf-8 -*-</span></a>
<a class="sourceLine" id="cb6-3" data-line-number="3"></a>
<a class="sourceLine" id="cb6-4" data-line-number="4"><span class="co">&quot;&quot;&quot;</span></a>
<a class="sourceLine" id="cb6-5" data-line-number="5"><span class="co">ZetCode PyQt5 tutorial </span></a>
<a class="sourceLine" id="cb6-6" data-line-number="6"></a>
<a class="sourceLine" id="cb6-7" data-line-number="7"><span class="co">In the example, we draw randomly 1000 red points </span></a>
<a class="sourceLine" id="cb6-8" data-line-number="8"><span class="co">on the window.</span></a>
<a class="sourceLine" id="cb6-9" data-line-number="9"></a>
<a class="sourceLine" id="cb6-10" data-line-number="10"><span class="co">Author: Jan Bodnar</span></a>
<a class="sourceLine" id="cb6-11" data-line-number="11"><span class="co">Website: zetcode.com </span></a>
<a class="sourceLine" id="cb6-12" data-line-number="12"><span class="co">Last edited: August 2017</span></a>
<a class="sourceLine" id="cb6-13" data-line-number="13"><span class="co">&quot;&quot;&quot;</span></a>
<a class="sourceLine" id="cb6-14" data-line-number="14"></a>
<a class="sourceLine" id="cb6-15" data-line-number="15"><span class="im">from</span> PyQt5.QtWidgets <span class="im">import</span> QWidget, QApplication</a>
<a class="sourceLine" id="cb6-16" data-line-number="16"><span class="im">from</span> PyQt5.QtGui <span class="im">import</span> QPainter</a>
<a class="sourceLine" id="cb6-17" data-line-number="17"><span class="im">from</span> PyQt5.QtCore <span class="im">import</span> Qt</a>
<a class="sourceLine" id="cb6-18" data-line-number="18"><span class="im">import</span> sys, random</a>
<a class="sourceLine" id="cb6-19" data-line-number="19"></a>
<a class="sourceLine" id="cb6-20" data-line-number="20"><span class="kw">class</span> Example(QWidget):</a>
<a class="sourceLine" id="cb6-21" data-line-number="21">    </a>
<a class="sourceLine" id="cb6-22" data-line-number="22">    <span class="kw">def</span> <span class="fu">__init__</span>(<span class="va">self</span>):</a>
<a class="sourceLine" id="cb6-23" data-line-number="23">        <span class="bu">super</span>().<span class="fu">__init__</span>()</a>
<a class="sourceLine" id="cb6-24" data-line-number="24">        </a>
<a class="sourceLine" id="cb6-25" data-line-number="25">        <span class="va">self</span>.initUI()</a>
<a class="sourceLine" id="cb6-26" data-line-number="26">        </a>
<a class="sourceLine" id="cb6-27" data-line-number="27">        </a>
<a class="sourceLine" id="cb6-28" data-line-number="28">    <span class="kw">def</span> initUI(<span class="va">self</span>):      </a>
<a class="sourceLine" id="cb6-29" data-line-number="29"></a>
<a class="sourceLine" id="cb6-30" data-line-number="30">        <span class="va">self</span>.setGeometry(<span class="dv">300</span>, <span class="dv">300</span>, <span class="dv">300</span>, <span class="dv">190</span>)</a>
<a class="sourceLine" id="cb6-31" data-line-number="31">        <span class="va">self</span>.setWindowTitle(<span class="st">&#39;Points&#39;</span>)</a>
<a class="sourceLine" id="cb6-32" data-line-number="32">        <span class="va">self</span>.show()</a>
<a class="sourceLine" id="cb6-33" data-line-number="33">        </a>
<a class="sourceLine" id="cb6-34" data-line-number="34"></a>
<a class="sourceLine" id="cb6-35" data-line-number="35">    <span class="kw">def</span> paintEvent(<span class="va">self</span>, e):</a>
<a class="sourceLine" id="cb6-36" data-line-number="36"></a>
<a class="sourceLine" id="cb6-37" data-line-number="37">        qp <span class="op">=</span> QPainter()</a>
<a class="sourceLine" id="cb6-38" data-line-number="38">        qp.begin(<span class="va">self</span>)</a>
<a class="sourceLine" id="cb6-39" data-line-number="39">        <span class="va">self</span>.drawPoints(qp)</a>
<a class="sourceLine" id="cb6-40" data-line-number="40">        qp.end()</a>
<a class="sourceLine" id="cb6-41" data-line-number="41">        </a>
<a class="sourceLine" id="cb6-42" data-line-number="42">        </a>
<a class="sourceLine" id="cb6-43" data-line-number="43">    <span class="kw">def</span> drawPoints(<span class="va">self</span>, qp):</a>
<a class="sourceLine" id="cb6-44" data-line-number="44">      </a>
<a class="sourceLine" id="cb6-45" data-line-number="45">        qp.setPen(Qt.red)</a>
<a class="sourceLine" id="cb6-46" data-line-number="46">        size <span class="op">=</span> <span class="va">self</span>.size()</a>
<a class="sourceLine" id="cb6-47" data-line-number="47">        </a>
<a class="sourceLine" id="cb6-48" data-line-number="48">        <span class="cf">for</span> i <span class="kw">in</span> <span class="bu">range</span>(<span class="dv">1000</span>):</a>
<a class="sourceLine" id="cb6-49" data-line-number="49">            x <span class="op">=</span> random.randint(<span class="dv">1</span>, size.width()<span class="op">-</span><span class="dv">1</span>)</a>
<a class="sourceLine" id="cb6-50" data-line-number="50">            y <span class="op">=</span> random.randint(<span class="dv">1</span>, size.height()<span class="op">-</span><span class="dv">1</span>)</a>
<a class="sourceLine" id="cb6-51" data-line-number="51">            qp.drawPoint(x, y)     </a>
<a class="sourceLine" id="cb6-52" data-line-number="52">                </a>
<a class="sourceLine" id="cb6-53" data-line-number="53">        </a>
<a class="sourceLine" id="cb6-54" data-line-number="54"><span class="cf">if</span> <span class="va">__name__</span> <span class="op">==</span> <span class="st">&#39;__main__&#39;</span>:</a>
<a class="sourceLine" id="cb6-55" data-line-number="55">    </a>
<a class="sourceLine" id="cb6-56" data-line-number="56">    app <span class="op">=</span> QApplication(sys.argv)</a>
<a class="sourceLine" id="cb6-57" data-line-number="57">    ex <span class="op">=</span> Example()</a>
<a class="sourceLine" id="cb6-58" data-line-number="58">    sys.exit(app.exec_())</a></code></pre></div>
<p>我们在窗口里随机的画出了1000个点。</p>
<pre><code class="language-python">qp.setPen(Qt.red)</code></pre>
<p>设置笔的颜色为红色，使用的是预定义好的颜色。</p>
<pre><code class="language-python">size = self.size()</code></pre>
<p>每次更改窗口大小，都会产生绘画事件，从<code>size()</code>方法里获得当前窗口的大小，然后把产生的点随机的分配到窗口的所有位置上。</p>
<pre><code class="language-python">qp.drawPoint(x, y)</code></pre>
<p><code>drawPoint()</code>方法绘图。</p>
<p>程序展示：</p>
<figure>
<img class="whitelist" src="docs/PyQt5/images/9-points.png" alt="points" />
</figure>
<h1 id="颜色">颜色</h1>
<p>颜色是一个物体显示的RGB的混合色。RBG值的范围是0<sub>255。我们有很多方式去定义一个颜色，最常见的方式就是RGB和16进制表示法，也可以使用RGBA，增加了一个透明度的选项，透明度值的范围是0</sub>1，0代表完全透明。</p>
<div class="sourceCode" id="cb10"><pre><code class="language-python"><a class="sourceLine" id="cb10-1" data-line-number="1"><span class="co">#!/usr/bin/python3</span></a>
<a class="sourceLine" id="cb10-2" data-line-number="2"><span class="co"># -*- coding: utf-8 -*-</span></a>
<a class="sourceLine" id="cb10-3" data-line-number="3"></a>
<a class="sourceLine" id="cb10-4" data-line-number="4"><span class="co">&quot;&quot;&quot;</span></a>
<a class="sourceLine" id="cb10-5" data-line-number="5"><span class="co">ZetCode PyQt5 tutorial </span></a>
<a class="sourceLine" id="cb10-6" data-line-number="6"></a>
<a class="sourceLine" id="cb10-7" data-line-number="7"><span class="co">This example draws three rectangles in three</span></a>
<a class="sourceLine" id="cb10-8" data-line-number="8"><span class="co">#different colours. </span></a>
<a class="sourceLine" id="cb10-9" data-line-number="9"></a>
<a class="sourceLine" id="cb10-10" data-line-number="10"><span class="co">Author: Jan Bodnar</span></a>
<a class="sourceLine" id="cb10-11" data-line-number="11"><span class="co">Website: zetcode.com </span></a>
<a class="sourceLine" id="cb10-12" data-line-number="12"><span class="co">Last edited: August 2017</span></a>
<a class="sourceLine" id="cb10-13" data-line-number="13"><span class="co">&quot;&quot;&quot;</span></a>
<a class="sourceLine" id="cb10-14" data-line-number="14"></a>
<a class="sourceLine" id="cb10-15" data-line-number="15"><span class="im">from</span> PyQt5.QtWidgets <span class="im">import</span> QWidget, QApplication</a>
<a class="sourceLine" id="cb10-16" data-line-number="16"><span class="im">from</span> PyQt5.QtGui <span class="im">import</span> QPainter, QColor, QBrush</a>
<a class="sourceLine" id="cb10-17" data-line-number="17"><span class="im">import</span> sys</a>
<a class="sourceLine" id="cb10-18" data-line-number="18"></a>
<a class="sourceLine" id="cb10-19" data-line-number="19"><span class="kw">class</span> Example(QWidget):</a>
<a class="sourceLine" id="cb10-20" data-line-number="20">    </a>
<a class="sourceLine" id="cb10-21" data-line-number="21">    <span class="kw">def</span> <span class="fu">__init__</span>(<span class="va">self</span>):</a>
<a class="sourceLine" id="cb10-22" data-line-number="22">        <span class="bu">super</span>().<span class="fu">__init__</span>()</a>
<a class="sourceLine" id="cb10-23" data-line-number="23">        </a>
<a class="sourceLine" id="cb10-24" data-line-number="24">        <span class="va">self</span>.initUI()</a>
<a class="sourceLine" id="cb10-25" data-line-number="25">        </a>
<a class="sourceLine" id="cb10-26" data-line-number="26">        </a>
<a class="sourceLine" id="cb10-27" data-line-number="27">    <span class="kw">def</span> initUI(<span class="va">self</span>):      </a>
<a class="sourceLine" id="cb10-28" data-line-number="28"></a>
<a class="sourceLine" id="cb10-29" data-line-number="29">        <span class="va">self</span>.setGeometry(<span class="dv">300</span>, <span class="dv">300</span>, <span class="dv">350</span>, <span class="dv">100</span>)</a>
<a class="sourceLine" id="cb10-30" data-line-number="30">        <span class="va">self</span>.setWindowTitle(<span class="st">&#39;Colours&#39;</span>)</a>
<a class="sourceLine" id="cb10-31" data-line-number="31">        <span class="va">self</span>.show()</a>
<a class="sourceLine" id="cb10-32" data-line-number="32"></a>
<a class="sourceLine" id="cb10-33" data-line-number="33"></a>
<a class="sourceLine" id="cb10-34" data-line-number="34">    <span class="kw">def</span> paintEvent(<span class="va">self</span>, e):</a>
<a class="sourceLine" id="cb10-35" data-line-number="35"></a>
<a class="sourceLine" id="cb10-36" data-line-number="36">        qp <span class="op">=</span> QPainter()</a>
<a class="sourceLine" id="cb10-37" data-line-number="37">        qp.begin(<span class="va">self</span>)</a>
<a class="sourceLine" id="cb10-38" data-line-number="38">        <span class="va">self</span>.drawRectangles(qp)</a>
<a class="sourceLine" id="cb10-39" data-line-number="39">        qp.end()</a>
<a class="sourceLine" id="cb10-40" data-line-number="40"></a>
<a class="sourceLine" id="cb10-41" data-line-number="41">        </a>
<a class="sourceLine" id="cb10-42" data-line-number="42">    <span class="kw">def</span> drawRectangles(<span class="va">self</span>, qp):</a>
<a class="sourceLine" id="cb10-43" data-line-number="43">      </a>
<a class="sourceLine" id="cb10-44" data-line-number="44">        col <span class="op">=</span> QColor(<span class="dv">0</span>, <span class="dv">0</span>, <span class="dv">0</span>)</a>
<a class="sourceLine" id="cb10-45" data-line-number="45">        col.setNamedColor(<span class="st">&#39;#d4d4d4&#39;</span>)</a>
<a class="sourceLine" id="cb10-46" data-line-number="46">        qp.setPen(col)</a>
<a class="sourceLine" id="cb10-47" data-line-number="47"></a>
<a class="sourceLine" id="cb10-48" data-line-number="48">        qp.setBrush(QColor(<span class="dv">200</span>, <span class="dv">0</span>, <span class="dv">0</span>))</a>
<a class="sourceLine" id="cb10-49" data-line-number="49">        qp.drawRect(<span class="dv">10</span>, <span class="dv">15</span>, <span class="dv">90</span>, <span class="dv">60</span>)</a>
<a class="sourceLine" id="cb10-50" data-line-number="50"></a>
<a class="sourceLine" id="cb10-51" data-line-number="51">        qp.setBrush(QColor(<span class="dv">255</span>, <span class="dv">80</span>, <span class="dv">0</span>, <span class="dv">160</span>))</a>
<a class="sourceLine" id="cb10-52" data-line-number="52">        qp.drawRect(<span class="dv">130</span>, <span class="dv">15</span>, <span class="dv">90</span>, <span class="dv">60</span>)</a>
<a class="sourceLine" id="cb10-53" data-line-number="53"></a>
<a class="sourceLine" id="cb10-54" data-line-number="54">        qp.setBrush(QColor(<span class="dv">25</span>, <span class="dv">0</span>, <span class="dv">90</span>, <span class="dv">200</span>))</a>
<a class="sourceLine" id="cb10-55" data-line-number="55">        qp.drawRect(<span class="dv">250</span>, <span class="dv">15</span>, <span class="dv">90</span>, <span class="dv">60</span>)</a>
<a class="sourceLine" id="cb10-56" data-line-number="56">              </a>
<a class="sourceLine" id="cb10-57" data-line-number="57">        </a>
<a class="sourceLine" id="cb10-58" data-line-number="58"><span class="cf">if</span> <span class="va">__name__</span> <span class="op">==</span> <span class="st">&#39;__main__&#39;</span>:</a>
<a class="sourceLine" id="cb10-59" data-line-number="59">    </a>
<a class="sourceLine" id="cb10-60" data-line-number="60">    app <span class="op">=</span> QApplication(sys.argv)</a>
<a class="sourceLine" id="cb10-61" data-line-number="61">    ex <span class="op">=</span> Example()</a>
<a class="sourceLine" id="cb10-62" data-line-number="62">    sys.exit(app.exec_())</a></code></pre></div>
<p>我们画出了三个颜色的矩形。</p>
<pre><code class="language-python">color = QColor(0, 0, 0)
color.setNamedColor(&#39;#d4d4d4&#39;)</code></pre>
<p>使用16进制的方式定义一个颜色。</p>
<pre><code class="language-python">qp.setBrush(QColor(200, 0, 0))
qp.drawRect(10, 15, 90, 60)</code></pre>
<p>定义了一个笔刷，并画出了一个矩形。笔刷是用来画一个物体的背景。<code>drawRect()</code>有四个参数，分别是矩形的x、y、w、h。 然后用笔刷和矩形进行绘画。</p>
<p>程序展示：</p>
<figure>
<img class="whitelist" src="docs/PyQt5/images/9-colours.png" alt="colours" />
</figure>
<h2 id="qpen">QPen</h2>
<p><code>QPen</code>是基本的绘画对象，能用来画直线、曲线、矩形框、椭圆、多边形和其他形状。</p>
<div class="sourceCode" id="cb13"><pre><code class="language-python"><a class="sourceLine" id="cb13-1" data-line-number="1"><span class="co">#!/usr/bin/python3</span></a>
<a class="sourceLine" id="cb13-2" data-line-number="2"><span class="co"># -*- coding: utf-8 -*-</span></a>
<a class="sourceLine" id="cb13-3" data-line-number="3"></a>
<a class="sourceLine" id="cb13-4" data-line-number="4"><span class="co">&quot;&quot;&quot;</span></a>
<a class="sourceLine" id="cb13-5" data-line-number="5"><span class="co">ZetCode PyQt5 tutorial </span></a>
<a class="sourceLine" id="cb13-6" data-line-number="6"></a>
<a class="sourceLine" id="cb13-7" data-line-number="7"><span class="co">In this example we draw 6 lines using</span></a>
<a class="sourceLine" id="cb13-8" data-line-number="8"><span class="co">different pen styles. </span></a>
<a class="sourceLine" id="cb13-9" data-line-number="9"></a>
<a class="sourceLine" id="cb13-10" data-line-number="10"><span class="co">Author: Jan Bodnar</span></a>
<a class="sourceLine" id="cb13-11" data-line-number="11"><span class="co">Website: zetcode.com </span></a>
<a class="sourceLine" id="cb13-12" data-line-number="12"><span class="co">Last edited: August 2017</span></a>
<a class="sourceLine" id="cb13-13" data-line-number="13"><span class="co">&quot;&quot;&quot;</span></a>
<a class="sourceLine" id="cb13-14" data-line-number="14"></a>
<a class="sourceLine" id="cb13-15" data-line-number="15"><span class="im">from</span> PyQt5.QtWidgets <span class="im">import</span> QWidget, QApplication</a>
<a class="sourceLine" id="cb13-16" data-line-number="16"><span class="im">from</span> PyQt5.QtGui <span class="im">import</span> QPainter, QPen</a>
<a class="sourceLine" id="cb13-17" data-line-number="17"><span class="im">from</span> PyQt5.QtCore <span class="im">import</span> Qt</a>
<a class="sourceLine" id="cb13-18" data-line-number="18"><span class="im">import</span> sys</a>
<a class="sourceLine" id="cb13-19" data-line-number="19"></a>
<a class="sourceLine" id="cb13-20" data-line-number="20"><span class="kw">class</span> Example(QWidget):</a>
<a class="sourceLine" id="cb13-21" data-line-number="21">    </a>
<a class="sourceLine" id="cb13-22" data-line-number="22">    <span class="kw">def</span> <span class="fu">__init__</span>(<span class="va">self</span>):</a>
<a class="sourceLine" id="cb13-23" data-line-number="23">        <span class="bu">super</span>().<span class="fu">__init__</span>()</a>
<a class="sourceLine" id="cb13-24" data-line-number="24">        </a>
<a class="sourceLine" id="cb13-25" data-line-number="25">        <span class="va">self</span>.initUI()</a>
<a class="sourceLine" id="cb13-26" data-line-number="26">        </a>
<a class="sourceLine" id="cb13-27" data-line-number="27">        </a>
<a class="sourceLine" id="cb13-28" data-line-number="28">    <span class="kw">def</span> initUI(<span class="va">self</span>):      </a>
<a class="sourceLine" id="cb13-29" data-line-number="29"></a>
<a class="sourceLine" id="cb13-30" data-line-number="30">        <span class="va">self</span>.setGeometry(<span class="dv">300</span>, <span class="dv">300</span>, <span class="dv">280</span>, <span class="dv">270</span>)</a>
<a class="sourceLine" id="cb13-31" data-line-number="31">        <span class="va">self</span>.setWindowTitle(<span class="st">&#39;Pen styles&#39;</span>)</a>
<a class="sourceLine" id="cb13-32" data-line-number="32">        <span class="va">self</span>.show()</a>
<a class="sourceLine" id="cb13-33" data-line-number="33">        </a>
<a class="sourceLine" id="cb13-34" data-line-number="34"></a>
<a class="sourceLine" id="cb13-35" data-line-number="35">    <span class="kw">def</span> paintEvent(<span class="va">self</span>, e):</a>
<a class="sourceLine" id="cb13-36" data-line-number="36"></a>
<a class="sourceLine" id="cb13-37" data-line-number="37">        qp <span class="op">=</span> QPainter()</a>
<a class="sourceLine" id="cb13-38" data-line-number="38">        qp.begin(<span class="va">self</span>)</a>
<a class="sourceLine" id="cb13-39" data-line-number="39">        <span class="va">self</span>.drawLines(qp)</a>
<a class="sourceLine" id="cb13-40" data-line-number="40">        qp.end()</a>
<a class="sourceLine" id="cb13-41" data-line-number="41">        </a>
<a class="sourceLine" id="cb13-42" data-line-number="42">        </a>
<a class="sourceLine" id="cb13-43" data-line-number="43">    <span class="kw">def</span> drawLines(<span class="va">self</span>, qp):</a>
<a class="sourceLine" id="cb13-44" data-line-number="44">      </a>
<a class="sourceLine" id="cb13-45" data-line-number="45">        pen <span class="op">=</span> QPen(Qt.black, <span class="dv">2</span>, Qt.SolidLine)</a>
<a class="sourceLine" id="cb13-46" data-line-number="46"></a>
<a class="sourceLine" id="cb13-47" data-line-number="47">        qp.setPen(pen)</a>
<a class="sourceLine" id="cb13-48" data-line-number="48">        qp.drawLine(<span class="dv">20</span>, <span class="dv">40</span>, <span class="dv">250</span>, <span class="dv">40</span>)</a>
<a class="sourceLine" id="cb13-49" data-line-number="49"></a>
<a class="sourceLine" id="cb13-50" data-line-number="50">        pen.setStyle(Qt.DashLine)</a>
<a class="sourceLine" id="cb13-51" data-line-number="51">        qp.setPen(pen)</a>
<a class="sourceLine" id="cb13-52" data-line-number="52">        qp.drawLine(<span class="dv">20</span>, <span class="dv">80</span>, <span class="dv">250</span>, <span class="dv">80</span>)</a>
<a class="sourceLine" id="cb13-53" data-line-number="53"></a>
<a class="sourceLine" id="cb13-54" data-line-number="54">        pen.setStyle(Qt.DashDotLine)</a>
<a class="sourceLine" id="cb13-55" data-line-number="55">        qp.setPen(pen)</a>
<a class="sourceLine" id="cb13-56" data-line-number="56">        qp.drawLine(<span class="dv">20</span>, <span class="dv">120</span>, <span class="dv">250</span>, <span class="dv">120</span>)</a>
<a class="sourceLine" id="cb13-57" data-line-number="57"></a>
<a class="sourceLine" id="cb13-58" data-line-number="58">        pen.setStyle(Qt.DotLine)</a>
<a class="sourceLine" id="cb13-59" data-line-number="59">        qp.setPen(pen)</a>
<a class="sourceLine" id="cb13-60" data-line-number="60">        qp.drawLine(<span class="dv">20</span>, <span class="dv">160</span>, <span class="dv">250</span>, <span class="dv">160</span>)</a>
<a class="sourceLine" id="cb13-61" data-line-number="61"></a>
<a class="sourceLine" id="cb13-62" data-line-number="62">        pen.setStyle(Qt.DashDotDotLine)</a>
<a class="sourceLine" id="cb13-63" data-line-number="63">        qp.setPen(pen)</a>
<a class="sourceLine" id="cb13-64" data-line-number="64">        qp.drawLine(<span class="dv">20</span>, <span class="dv">200</span>, <span class="dv">250</span>, <span class="dv">200</span>)</a>
<a class="sourceLine" id="cb13-65" data-line-number="65"></a>
<a class="sourceLine" id="cb13-66" data-line-number="66">        pen.setStyle(Qt.CustomDashLine)</a>
<a class="sourceLine" id="cb13-67" data-line-number="67">        pen.setDashPattern([<span class="dv">1</span>, <span class="dv">4</span>, <span class="dv">5</span>, <span class="dv">4</span>])</a>
<a class="sourceLine" id="cb13-68" data-line-number="68">        qp.setPen(pen)</a>
<a class="sourceLine" id="cb13-69" data-line-number="69">        qp.drawLine(<span class="dv">20</span>, <span class="dv">240</span>, <span class="dv">250</span>, <span class="dv">240</span>)</a>
<a class="sourceLine" id="cb13-70" data-line-number="70">              </a>
<a class="sourceLine" id="cb13-71" data-line-number="71">        </a>
<a class="sourceLine" id="cb13-72" data-line-number="72"><span class="cf">if</span> <span class="va">__name__</span> <span class="op">==</span> <span class="st">&#39;__main__&#39;</span>:</a>
<a class="sourceLine" id="cb13-73" data-line-number="73">    </a>
<a class="sourceLine" id="cb13-74" data-line-number="74">    app <span class="op">=</span> QApplication(sys.argv)</a>
<a class="sourceLine" id="cb13-75" data-line-number="75">    ex <span class="op">=</span> Example()</a>
<a class="sourceLine" id="cb13-76" data-line-number="76">    sys.exit(app.exec_())</a></code></pre></div>
<p>在这个例子里，我们用不同的笔画了6条直线。PyQt5有五个预定义的笔，另外一个笔的样式使我们自定义的。</p>
<pre><code class="language-python">pen = QPen(Qt.black, 2, Qt.SolidLine)</code></pre>
<p>新建一个<code>QPen</code>对象，设置颜色黑色，宽2像素，这样就能看出来各个笔样式的区别。<code>Qt.SolidLine</code>是预定义样式的一种。</p>
<pre><code class="language-python">pen.setStyle(Qt.CustomDashLine)
pen.setDashPattern([1, 4, 5, 4])
qp.setPen(pen)</code></pre>
<p>这里我们自定义了一个笔的样式。定义为<code>Qt.CustomDashLine</code>然后调用<code>setDashPattern()</code>方法。数字列表是线的样式，要求必须是个数为奇数，奇数位定义的是空格，偶数位为线长，数字越大，空格或线长越大，比如本例的就是1像素线，4像素空格，5像素线，4像素空格。</p>
<p>程序展示：</p>
<figure>
<img class="whitelist" src="docs/PyQt5/images/9-penstyles.png" alt="pen styles" />
</figure>
<h2 id="qbrush">QBrush</h2>
<p><code>QBrush</code>也是图像的一个基本元素。是用来填充一些物体的背景图用的，比如矩形，椭圆，多边形等。有三种类型：预定义、渐变和纹理。</p>
<div class="sourceCode" id="cb16"><pre><code class="language-python"><a class="sourceLine" id="cb16-1" data-line-number="1"><span class="co">#!/usr/bin/python3</span></a>
<a class="sourceLine" id="cb16-2" data-line-number="2"><span class="co"># -*- coding: utf-8 -*-</span></a>
<a class="sourceLine" id="cb16-3" data-line-number="3"></a>
<a class="sourceLine" id="cb16-4" data-line-number="4"><span class="co">&quot;&quot;&quot;</span></a>
<a class="sourceLine" id="cb16-5" data-line-number="5"><span class="co">ZetCode PyQt5 tutorial </span></a>
<a class="sourceLine" id="cb16-6" data-line-number="6"></a>
<a class="sourceLine" id="cb16-7" data-line-number="7"><span class="co">This example draws nine rectangles in different</span></a>
<a class="sourceLine" id="cb16-8" data-line-number="8"><span class="co">brush styles.</span></a>
<a class="sourceLine" id="cb16-9" data-line-number="9"></a>
<a class="sourceLine" id="cb16-10" data-line-number="10"><span class="co">Author: Jan Bodnar</span></a>
<a class="sourceLine" id="cb16-11" data-line-number="11"><span class="co">Website: zetcode.com </span></a>
<a class="sourceLine" id="cb16-12" data-line-number="12"><span class="co">Last edited: August 2017</span></a>
<a class="sourceLine" id="cb16-13" data-line-number="13"><span class="co">&quot;&quot;&quot;</span></a>
<a class="sourceLine" id="cb16-14" data-line-number="14"></a>
<a class="sourceLine" id="cb16-15" data-line-number="15"><span class="im">from</span> PyQt5.QtWidgets <span class="im">import</span> QWidget, QApplication</a>
<a class="sourceLine" id="cb16-16" data-line-number="16"><span class="im">from</span> PyQt5.QtGui <span class="im">import</span> QPainter, QBrush</a>
<a class="sourceLine" id="cb16-17" data-line-number="17"><span class="im">from</span> PyQt5.QtCore <span class="im">import</span> Qt</a>
<a class="sourceLine" id="cb16-18" data-line-number="18"><span class="im">import</span> sys</a>
<a class="sourceLine" id="cb16-19" data-line-number="19"></a>
<a class="sourceLine" id="cb16-20" data-line-number="20"><span class="kw">class</span> Example(QWidget):</a>
<a class="sourceLine" id="cb16-21" data-line-number="21">    </a>
<a class="sourceLine" id="cb16-22" data-line-number="22">    <span class="kw">def</span> <span class="fu">__init__</span>(<span class="va">self</span>):</a>
<a class="sourceLine" id="cb16-23" data-line-number="23">        <span class="bu">super</span>().<span class="fu">__init__</span>()</a>
<a class="sourceLine" id="cb16-24" data-line-number="24">        </a>
<a class="sourceLine" id="cb16-25" data-line-number="25">        <span class="va">self</span>.initUI()</a>
<a class="sourceLine" id="cb16-26" data-line-number="26">        </a>
<a class="sourceLine" id="cb16-27" data-line-number="27">        </a>
<a class="sourceLine" id="cb16-28" data-line-number="28">    <span class="kw">def</span> initUI(<span class="va">self</span>):      </a>
<a class="sourceLine" id="cb16-29" data-line-number="29"></a>
<a class="sourceLine" id="cb16-30" data-line-number="30">        <span class="va">self</span>.setGeometry(<span class="dv">300</span>, <span class="dv">300</span>, <span class="dv">355</span>, <span class="dv">280</span>)</a>
<a class="sourceLine" id="cb16-31" data-line-number="31">        <span class="va">self</span>.setWindowTitle(<span class="st">&#39;Brushes&#39;</span>)</a>
<a class="sourceLine" id="cb16-32" data-line-number="32">        <span class="va">self</span>.show()</a>
<a class="sourceLine" id="cb16-33" data-line-number="33">        </a>
<a class="sourceLine" id="cb16-34" data-line-number="34"></a>
<a class="sourceLine" id="cb16-35" data-line-number="35">    <span class="kw">def</span> paintEvent(<span class="va">self</span>, e):</a>
<a class="sourceLine" id="cb16-36" data-line-number="36"></a>
<a class="sourceLine" id="cb16-37" data-line-number="37">        qp <span class="op">=</span> QPainter()</a>
<a class="sourceLine" id="cb16-38" data-line-number="38">        qp.begin(<span class="va">self</span>)</a>
<a class="sourceLine" id="cb16-39" data-line-number="39">        <span class="va">self</span>.drawBrushes(qp)</a>
<a class="sourceLine" id="cb16-40" data-line-number="40">        qp.end()</a>
<a class="sourceLine" id="cb16-41" data-line-number="41">        </a>
<a class="sourceLine" id="cb16-42" data-line-number="42">        </a>
<a class="sourceLine" id="cb16-43" data-line-number="43">    <span class="kw">def</span> drawBrushes(<span class="va">self</span>, qp):</a>
<a class="sourceLine" id="cb16-44" data-line-number="44">      </a>
<a class="sourceLine" id="cb16-45" data-line-number="45">        brush <span class="op">=</span> QBrush(Qt.SolidPattern)</a>
<a class="sourceLine" id="cb16-46" data-line-number="46">        qp.setBrush(brush)</a>
<a class="sourceLine" id="cb16-47" data-line-number="47">        qp.drawRect(<span class="dv">10</span>, <span class="dv">15</span>, <span class="dv">90</span>, <span class="dv">60</span>)</a>
<a class="sourceLine" id="cb16-48" data-line-number="48"></a>
<a class="sourceLine" id="cb16-49" data-line-number="49">        brush.setStyle(Qt.Dense1Pattern)</a>
<a class="sourceLine" id="cb16-50" data-line-number="50">        qp.setBrush(brush)</a>
<a class="sourceLine" id="cb16-51" data-line-number="51">        qp.drawRect(<span class="dv">130</span>, <span class="dv">15</span>, <span class="dv">90</span>, <span class="dv">60</span>)</a>
<a class="sourceLine" id="cb16-52" data-line-number="52"></a>
<a class="sourceLine" id="cb16-53" data-line-number="53">        brush.setStyle(Qt.Dense2Pattern)</a>
<a class="sourceLine" id="cb16-54" data-line-number="54">        qp.setBrush(brush)</a>
<a class="sourceLine" id="cb16-55" data-line-number="55">        qp.drawRect(<span class="dv">250</span>, <span class="dv">15</span>, <span class="dv">90</span>, <span class="dv">60</span>)</a>
<a class="sourceLine" id="cb16-56" data-line-number="56"></a>
<a class="sourceLine" id="cb16-57" data-line-number="57">        brush.setStyle(Qt.DiagCrossPattern)</a>
<a class="sourceLine" id="cb16-58" data-line-number="58">        qp.setBrush(brush)</a>
<a class="sourceLine" id="cb16-59" data-line-number="59">        qp.drawRect(<span class="dv">10</span>, <span class="dv">105</span>, <span class="dv">90</span>, <span class="dv">60</span>)</a>
<a class="sourceLine" id="cb16-60" data-line-number="60"></a>
<a class="sourceLine" id="cb16-61" data-line-number="61">        brush.setStyle(Qt.Dense5Pattern)</a>
<a class="sourceLine" id="cb16-62" data-line-number="62">        qp.setBrush(brush)</a>
<a class="sourceLine" id="cb16-63" data-line-number="63">        qp.drawRect(<span class="dv">130</span>, <span class="dv">105</span>, <span class="dv">90</span>, <span class="dv">60</span>)</a>
<a class="sourceLine" id="cb16-64" data-line-number="64"></a>
<a class="sourceLine" id="cb16-65" data-line-number="65">        brush.setStyle(Qt.Dense6Pattern)</a>
<a class="sourceLine" id="cb16-66" data-line-number="66">        qp.setBrush(brush)</a>
<a class="sourceLine" id="cb16-67" data-line-number="67">        qp.drawRect(<span class="dv">250</span>, <span class="dv">105</span>, <span class="dv">90</span>, <span class="dv">60</span>)</a>
<a class="sourceLine" id="cb16-68" data-line-number="68"></a>
<a class="sourceLine" id="cb16-69" data-line-number="69">        brush.setStyle(Qt.HorPattern)</a>
<a class="sourceLine" id="cb16-70" data-line-number="70">        qp.setBrush(brush)</a>
<a class="sourceLine" id="cb16-71" data-line-number="71">        qp.drawRect(<span class="dv">10</span>, <span class="dv">195</span>, <span class="dv">90</span>, <span class="dv">60</span>)</a>
<a class="sourceLine" id="cb16-72" data-line-number="72"></a>
<a class="sourceLine" id="cb16-73" data-line-number="73">        brush.setStyle(Qt.VerPattern)</a>
<a class="sourceLine" id="cb16-74" data-line-number="74">        qp.setBrush(brush)</a>
<a class="sourceLine" id="cb16-75" data-line-number="75">        qp.drawRect(<span class="dv">130</span>, <span class="dv">195</span>, <span class="dv">90</span>, <span class="dv">60</span>)</a>
<a class="sourceLine" id="cb16-76" data-line-number="76"></a>
<a class="sourceLine" id="cb16-77" data-line-number="77">        brush.setStyle(Qt.BDiagPattern)</a>
<a class="sourceLine" id="cb16-78" data-line-number="78">        qp.setBrush(brush)</a>
<a class="sourceLine" id="cb16-79" data-line-number="79">        qp.drawRect(<span class="dv">250</span>, <span class="dv">195</span>, <span class="dv">90</span>, <span class="dv">60</span>)</a>
<a class="sourceLine" id="cb16-80" data-line-number="80">              </a>
<a class="sourceLine" id="cb16-81" data-line-number="81">        </a>
<a class="sourceLine" id="cb16-82" data-line-number="82"><span class="cf">if</span> <span class="va">__name__</span> <span class="op">==</span> <span class="st">&#39;__main__&#39;</span>:</a>
<a class="sourceLine" id="cb16-83" data-line-number="83">    </a>
<a class="sourceLine" id="cb16-84" data-line-number="84">    app <span class="op">=</span> QApplication(sys.argv)</a>
<a class="sourceLine" id="cb16-85" data-line-number="85">    ex <span class="op">=</span> Example()</a>
<a class="sourceLine" id="cb16-86" data-line-number="86">    sys.exit(app.exec_())</a></code></pre></div>
<p>我们画了9个不同的矩形。</p>
<pre><code class="language-python">brush = QBrush(Qt.SolidPattern)
qp.setBrush(brush)
qp.drawRect(10, 15, 90, 60)</code></pre>
<p>创建了一个笔刷对象，添加笔刷样式，然后调用<code>drawRect()</code>方法画图。</p>
<p>程序展示：</p>
<figure>
<img class="whitelist" src="docs/PyQt5/images/9-brushes.png" alt="brushes" />
</figure>
<h2 id="贝塞尔曲线">贝塞尔曲线</h2>
<p>噩梦可以使用PyQt5的<code>QPainterPath</code>创建贝塞尔曲线。绘画路径是由许多构建图形的对象，具体表现就是一些线的形状，比如矩形，椭圆，线和曲线。</p>
<div class="sourceCode" id="cb18"><pre><code class="language-python"><a class="sourceLine" id="cb18-1" data-line-number="1"></a>
<a class="sourceLine" id="cb18-2" data-line-number="2"><span class="co">#!/usr/bin/python3</span></a>
<a class="sourceLine" id="cb18-3" data-line-number="3"><span class="co"># -*- coding: utf-8 -*-</span></a>
<a class="sourceLine" id="cb18-4" data-line-number="4"></a>
<a class="sourceLine" id="cb18-5" data-line-number="5"><span class="co">&quot;&quot;&quot;</span></a>
<a class="sourceLine" id="cb18-6" data-line-number="6"><span class="co">ZetCode PyQt5 tutorial </span></a>
<a class="sourceLine" id="cb18-7" data-line-number="7"></a>
<a class="sourceLine" id="cb18-8" data-line-number="8"><span class="co">This program draws a Bézier curve with </span></a>
<a class="sourceLine" id="cb18-9" data-line-number="9"><span class="co">QPainterPath.</span></a>
<a class="sourceLine" id="cb18-10" data-line-number="10"></a>
<a class="sourceLine" id="cb18-11" data-line-number="11"><span class="co">Author: Jan Bodnar</span></a>
<a class="sourceLine" id="cb18-12" data-line-number="12"><span class="co">Website: zetcode.com </span></a>
<a class="sourceLine" id="cb18-13" data-line-number="13"><span class="co">Last edited: August 2017</span></a>
<a class="sourceLine" id="cb18-14" data-line-number="14"><span class="co">&quot;&quot;&quot;</span></a>
<a class="sourceLine" id="cb18-15" data-line-number="15"></a>
<a class="sourceLine" id="cb18-16" data-line-number="16"><span class="im">from</span> PyQt5.QtWidgets <span class="im">import</span> QWidget, QApplication</a>
<a class="sourceLine" id="cb18-17" data-line-number="17"><span class="im">from</span> PyQt5.QtGui <span class="im">import</span> QPainter, QPainterPath</a>
<a class="sourceLine" id="cb18-18" data-line-number="18"><span class="im">from</span> PyQt5.QtCore <span class="im">import</span> Qt</a>
<a class="sourceLine" id="cb18-19" data-line-number="19"><span class="im">import</span> sys</a>
<a class="sourceLine" id="cb18-20" data-line-number="20"></a>
<a class="sourceLine" id="cb18-21" data-line-number="21"><span class="kw">class</span> Example(QWidget):</a>
<a class="sourceLine" id="cb18-22" data-line-number="22">    </a>
<a class="sourceLine" id="cb18-23" data-line-number="23">    <span class="kw">def</span> <span class="fu">__init__</span>(<span class="va">self</span>):</a>
<a class="sourceLine" id="cb18-24" data-line-number="24">        <span class="bu">super</span>().<span class="fu">__init__</span>()</a>
<a class="sourceLine" id="cb18-25" data-line-number="25">        </a>
<a class="sourceLine" id="cb18-26" data-line-number="26">        <span class="va">self</span>.initUI()</a>
<a class="sourceLine" id="cb18-27" data-line-number="27">        </a>
<a class="sourceLine" id="cb18-28" data-line-number="28">        </a>
<a class="sourceLine" id="cb18-29" data-line-number="29">    <span class="kw">def</span> initUI(<span class="va">self</span>):      </a>
<a class="sourceLine" id="cb18-30" data-line-number="30"></a>
<a class="sourceLine" id="cb18-31" data-line-number="31">        <span class="va">self</span>.setGeometry(<span class="dv">300</span>, <span class="dv">300</span>, <span class="dv">380</span>, <span class="dv">250</span>)</a>
<a class="sourceLine" id="cb18-32" data-line-number="32">        <span class="va">self</span>.setWindowTitle(<span class="st">&#39;Bézier curve&#39;</span>)</a>
<a class="sourceLine" id="cb18-33" data-line-number="33">        <span class="va">self</span>.show()</a>
<a class="sourceLine" id="cb18-34" data-line-number="34">        </a>
<a class="sourceLine" id="cb18-35" data-line-number="35"></a>
<a class="sourceLine" id="cb18-36" data-line-number="36">    <span class="kw">def</span> paintEvent(<span class="va">self</span>, e):</a>
<a class="sourceLine" id="cb18-37" data-line-number="37"></a>
<a class="sourceLine" id="cb18-38" data-line-number="38">        qp <span class="op">=</span> QPainter()</a>
<a class="sourceLine" id="cb18-39" data-line-number="39">        qp.begin(<span class="va">self</span>)</a>
<a class="sourceLine" id="cb18-40" data-line-number="40">        qp.setRenderHint(QPainter.Antialiasing)</a>
<a class="sourceLine" id="cb18-41" data-line-number="41">        <span class="va">self</span>.drawBezierCurve(qp)</a>
<a class="sourceLine" id="cb18-42" data-line-number="42">        qp.end()</a>
<a class="sourceLine" id="cb18-43" data-line-number="43">        </a>
<a class="sourceLine" id="cb18-44" data-line-number="44">        </a>
<a class="sourceLine" id="cb18-45" data-line-number="45">    <span class="kw">def</span> drawBezierCurve(<span class="va">self</span>, qp):</a>
<a class="sourceLine" id="cb18-46" data-line-number="46">      </a>
<a class="sourceLine" id="cb18-47" data-line-number="47">        path <span class="op">=</span> QPainterPath()</a>
<a class="sourceLine" id="cb18-48" data-line-number="48">        path.moveTo(<span class="dv">30</span>, <span class="dv">30</span>)</a>
<a class="sourceLine" id="cb18-49" data-line-number="49">        path.cubicTo(<span class="dv">30</span>, <span class="dv">30</span>, <span class="dv">200</span>, <span class="dv">350</span>, <span class="dv">350</span>, <span class="dv">30</span>)</a>
<a class="sourceLine" id="cb18-50" data-line-number="50">        </a>
<a class="sourceLine" id="cb18-51" data-line-number="51">        qp.drawPath(path)</a>
<a class="sourceLine" id="cb18-52" data-line-number="52">              </a>
<a class="sourceLine" id="cb18-53" data-line-number="53">        </a>
<a class="sourceLine" id="cb18-54" data-line-number="54"><span class="cf">if</span> <span class="va">__name__</span> <span class="op">==</span> <span class="st">&#39;__main__&#39;</span>:</a>
<a class="sourceLine" id="cb18-55" data-line-number="55">    </a>
<a class="sourceLine" id="cb18-56" data-line-number="56">    app <span class="op">=</span> QApplication(sys.argv)</a>
<a class="sourceLine" id="cb18-57" data-line-number="57">    ex <span class="op">=</span> Example()</a>
<a class="sourceLine" id="cb18-58" data-line-number="58">    sys.exit(app.exec_())</a></code></pre></div>
<p>这个示例中，我们画出了一个贝塞尔曲线。</p>
<div class="sourceCode" id="cb19"><pre><code class="language-python"><a class="sourceLine" id="cb19-1" data-line-number="1">path <span class="op">=</span> QPainterPath()</a>
<a class="sourceLine" id="cb19-2" data-line-number="2">path.moveTo(<span class="dv">30</span>, <span class="dv">30</span>)</a>
<a class="sourceLine" id="cb19-3" data-line-number="3">path.cubicTo(<span class="dv">30</span>, <span class="dv">30</span>, <span class="dv">200</span>, <span class="dv">350</span>, <span class="dv">350</span>, <span class="dv">30</span>)</a></code></pre></div>
<p>用<code>QPainterPath</code>路径创建贝塞尔曲线。使用<code>cubicTo()</code>方法生成，分别需要三个点：起始点，控制点和终止点。</p>
<div class="sourceCode" id="cb20"><pre><code class="language-python"><a class="sourceLine" id="cb20-1" data-line-number="1">qp.drawPath(path)</a></code></pre></div>
<p><code>drawPath()</code>绘制最后的图像。</p>
<p>程序展示：</p>
